package com.atguigu.adddemo;

public class NumberClass {
    private int num = 0;

    public synchronized void add() {

        while (num != 0) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        num++;
        System.out.println(Thread.currentThread().getName() + "线程，num的值是：" + num);
        this.notifyAll();
    }

    public synchronized void sub() {

        while (num != 1) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        num--;
        System.out.println(Thread.currentThread().getName() + "线程，num的值是：" + num);
        this.notifyAll();
    }
}
